package com.hcnow.common.utils

import com.rest.model.User
import org.apache.shiro.crypto.RandomNumberGenerator
import org.apache.shiro.crypto.SecureRandomNumberGenerator
import org.apache.shiro.crypto.hash.SimpleHash
import org.apache.shiro.util.ByteSource

object PasswordUtils{
    private val randomNumberGenerator: RandomNumberGenerator = SecureRandomNumberGenerator()
    private const val algorithmName:String = "md5" //加密方式
    private const val hashIterations:Int = 10 //加密次数
    private const val salt:String = "salt" //密码盐值

    /**
     * 密码加密
     */
    fun encryptPassword(user: User):Unit{
        user.password = SimpleHash(algorithmName, user.password,
                ByteSource.Util.bytes(randomNumberGenerator.nextBytes().toHex()),
                hashIterations).toHex()
    }


    /**
     * 验证用户密码
     */
    fun validationPassword(user: User,password:String):Boolean{
        val newPassword = SimpleHash(algorithmName, password,
                ByteSource.Util.bytes(randomNumberGenerator.nextBytes().toHex()),
                hashIterations).toHex()
        return newPassword == user.password
    }

}